package com.zhu.day.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RestController
public class UploadController {



    @RequestMapping("/upload")
    public Map<String, Object> upload(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "user", required = true) String user) throws IOException {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try (FileInputStream in = (FileInputStream) file.getInputStream();
             FileOutputStream out = new FileOutputStream("filePathAndName")) {

            //将Json对象解析为UserModel对象

            //保存文件到filePathAndName
            int hasRead = 0;
            byte[] bytes = new byte[1024];
            while ((hasRead = in.read(bytes)) > 0) {
                out.write(bytes, 0, hasRead);
            }
            objectObjectHashMap.put("ok",1);
        } catch (IOException e) {
            e.printStackTrace();
            objectObjectHashMap.put("ok",0);
        }
        return objectObjectHashMap;
    }

}
